﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using CQRS.Events;
using CQRS.Infrastructure.Hermes.Infrastructure.ServiceLocator;
using CQRS.Reports;
using CQRS.Sample.Domain.Commands;
using CQRS.Sample.Domain.ReportDtos;

namespace CQRS.Example.SampleApplication.Views
{
    public partial class PlaneDetailsForm : BaseForm
    {
        private readonly IBus<Command> _commandBus;
        private readonly IReportingRepository _reportingRepository;
        private Guid _planeId;

        public PlaneDetailsForm(IReportingRepository reportingRepository)
        {
            InitializeComponent();
            groupBoxRegister.Visible = false;
            _reportingRepository = reportingRepository;
        }

        public PlaneDetailsForm(IBus<Command> commandBus, IReportingRepository reportingRepository)
        {
            InitializeComponent();
            _commandBus = commandBus;
            groupBoxRegister.Visible = false;
            _reportingRepository = reportingRepository;
        }

        private void buttonCreatePlane_Click(object sender, EventArgs e)
        {
            long flightsMadeByPilot, yearManufactured;
            if (!ParseLong(textBoxFlightsMadeByPilot, out flightsMadeByPilot))
            {
                MessageBox.Show("Cannot create the pilot, cannot parse flighs made by pilot.");
                return;
            }
            if (!ParseLong(textBoxYearManufactured, out yearManufactured))
            {
                MessageBox.Show("Cannot create the plane, cannot parse the manufacture year.");
                return;
            }
            var createPlaneCommandHandler = new CreatePlaneCommand(textBoxPlaneName.Text, textBoxManufacturer.Text,
                                                                   yearManufactured,
                                                                   0, textBoxPilotName.Text, textBoxPilotSurname.Text,
                                                                   flightsMadeByPilot);
            _commandBus.Publish(createPlaneCommandHandler);
            _commandBus.Commit();
            Close();
        }

        internal void FillWithReport(PlaneDetailsReport planeDetailsReport)
        {
            groupBoxRegister.Visible = !planeDetailsReport.HadCrashed;
            buttonCreatePlane.Visible = false;
            textBoxPlaneName.Text = planeDetailsReport.PlaneName;
            textBoxManufacturer.Text = planeDetailsReport.Manufacturer;
            textBoxYearManufactured.Text = planeDetailsReport.YearManufactured.ToString();
            textBoxPilotSurname.Text = planeDetailsReport.PilotSurname;
            textBoxPilotName.Text = planeDetailsReport.PilotName;
            textBoxFlightsMadeByPilot.Text = planeDetailsReport.FlightsMadeByPilot.ToString();
            _planeId = planeDetailsReport.Id;
            pictureBoxCrashed.Visible = planeDetailsReport.HadCrashed;
            pictureBoxEmergency.Visible = planeDetailsReport.HadEmergencies;
            pictureBoxRepaired.Visible = planeDetailsReport.HadBeenRepaired;
        }

        private void buttonRegisterRepair_Click(object sender, EventArgs e)
        {
            var registerRepairForm = IoC.Resolve<RegisterRepairForm>();
            registerRepairForm.Init(_planeId);
            LoadDialog(registerRepairForm);
            UpdateView();
        }

        private void buttonRegisterEmergency_Click(object sender, EventArgs e)
        {
            var registerEmergencyForm = IoC.Resolve<RegisterEmergencyForm>();
            registerEmergencyForm.Init(_planeId);
            LoadDialog(registerEmergencyForm);
            UpdateView();
        }

        private void buttonRegisterCrash_Click(object sender, EventArgs e)
        {
            var registerCrashForm = IoC.Resolve<RegisterCrashForm>();
            registerCrashForm.Init(_planeId);
            LoadDialog(registerCrashForm);
            UpdateView();
        }

        private void pictureBoxEmergency_Click(object sender, EventArgs e)
        {
            IEnumerable<PlaneEmergencyReport> dataSource = _reportingRepository.GetByCriteria<PlaneEmergencyReport>(new QueryCriteria().AddCriteria("Id", _planeId));
            var popupGridForm = new PopupGridForm(dataSource, "Registered emergencies of the plane");
            popupGridForm.ShowDialog();
        }

        private void UpdateView()
        {
            PlaneDetailsReport planeDetailsReport = _reportingRepository
                .GetByCriteria<PlaneDetailsReport>(new QueryCriteria().AddCriteria("Id", _planeId))
                .ToList().First();
            FillWithReport(planeDetailsReport);
        }

        private void pictureBoxRepaired_Click(object sender, EventArgs e)
        {
            IEnumerable<PlaneRepairReport> dataSource = _reportingRepository.GetByCriteria<PlaneRepairReport>(new QueryCriteria().AddCriteria("Id", _planeId));
            var popupGridForm = new PopupGridForm(dataSource, "Registered repairs of the plane");
            popupGridForm.ShowDialog();
        }

        private void pictureBoxCrashed_Click(object sender, EventArgs e)
        {
            IEnumerable<PlaneCrashReport> dataSource = _reportingRepository.GetByCriteria<PlaneCrashReport>(new QueryCriteria().AddCriteria("Id", _planeId));
            var popupGridForm = new PopupGridForm(dataSource, "Plane crashes");
            popupGridForm.ShowDialog();
        }
    }
}